package gz.itcast.b_statement;

import gz.itcast.util.JdbcUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created 吴海杰 on 2017/2/27.
 */
public class Demo3 {
    @Test
    public void test1(){
        Connection conn=null;
        Statement stmt=null;
        try {
            conn= JdbcUtil.getConnection();
            stmt=conn.createStatement();
           String sql="SELECT *FROM student;";
            ResultSet rs=stmt.executeQuery(sql);
            /*boolean flag=rs.next();
            flag=rs.next();
            flag=rs.next();
            if(flag){
                int id=rs.getInt(1);
                String name=rs.getString(2);
                String gender =rs.getString(3);
                System.out.println(id+","+name+","+gender);
            }*/
            //遍历结果
            while ((rs.next())){
                int id=rs.getInt(1);
                String name=rs.getString(2);
                String gender =rs.getString(3);
                System.out.println(id+","+name+","+gender);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            JdbcUtil.close(conn,stmt);
        }
    }

}
